package com.hmall.search.listener;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.hmall.api.dto.OrderDetailDTO;
import com.hmall.api.po.Item;
import com.hmall.common.constant.EsConstant;
import com.hmall.common.constant.OverPayConstant;
import com.hmall.search.domain.dto.ItemDoc;
import lombok.RequiredArgsConstructor;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

@Component
@RequiredArgsConstructor
public class RabbitMqListener {

    private final RestHighLevelClient client;

    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(name = EsConstant.SAVE_QUEUE,durable = "true"),
            exchange =@Exchange(name = EsConstant.EXCHANGE,type = ExchangeTypes.DIRECT),
            key = EsConstant.SAVE
    ))
    public void listenSave(Item item) throws IOException {
        IndexRequest request = new IndexRequest("items").id(item.getId().toString());
        ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
        request.source(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        client.index(request, RequestOptions.DEFAULT);
    }

    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(name = EsConstant.UPDATE_QUEUE,durable = "true"),
            exchange =@Exchange(name = EsConstant.EXCHANGE,type = ExchangeTypes.DIRECT),
            key = EsConstant.UPDATE
    ))
    public void listenUpdate(Item item) throws IOException {
        UpdateRequest request = new UpdateRequest("items",item.getId().toString());
        ItemDoc itemDoc = BeanUtil.copyProperties(item, ItemDoc.class);
        request.doc(JSONUtil.toJsonStr(itemDoc), XContentType.JSON);
        client.update(request, RequestOptions.DEFAULT);
    }

    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(name = EsConstant.DELETE_QUEUE,durable = "true"),
            exchange =@Exchange(name = EsConstant.EXCHANGE,type = ExchangeTypes.DIRECT),
            key = EsConstant.DELETE
    ))
    public void listenDelete(Long id) throws IOException {
        DeleteRequest request = new DeleteRequest("items",id.toString());
        client.delete(request, RequestOptions.DEFAULT);
    }

    @RabbitListener(bindings =
    @QueueBinding(
            value = @Queue(name = EsConstant.DECREASE_STOCK,durable = "true"),
            exchange =@Exchange(name = EsConstant.EXCHANGE,type = ExchangeTypes.DIRECT),
            key = EsConstant.DECREASE_STOCK
    ))
    public void listenDecreaseStock(List<OrderDetailDTO> items) throws IOException {
//        BulkRequest request = new BulkRequest();
//        for (OrderDetailDTO item : items) {
//            GetRequest getRequest = new GetRequest("items").id(item.getItemId().toString());
//            GetResponse response = client.get(getRequest, RequestOptions.DEFAULT);
//            String json = response.getSourceAsString();
//            ItemDoc itemDoc = JSONUtil.toBean(json, ItemDoc.class);
//        }
//        List<Long> idList=List.of(546872L,561178L,577967L);
//        List<Item> items = itemService.listByIds(idList);
//        for (Item item : items) {
//            Integer price = item.getPrice();
//            request.add(new UpdateRequest("items",item.getId().toString())
//            .doc(Map.of("price",price-10000)));
//        }
//        client.bulk(request,RequestOptions.DEFAULT);

    }
}
